﻿using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace DAL
{
    public class UpdateData
    {
        DataTranferEntity trans = new DataTranferEntity();
        GetData data = new GetData();

        public void UpdateTaskList(long CustomerID, int OldUserID, int NewUserID)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                var query = db.DividingWorkings.Where(a => a.User_ID == OldUserID && a.Customer_ID == CustomerID).SingleOrDefault();
                query.User_ID = NewUserID;
                db.SaveChanges();
            }
            catch
            {
                return;
            }

        }

        public void UpdateKPIList(List<CustomerEntity> KPIList)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                foreach (CustomerEntity item in KPIList)
                {
                    var query = db.Customers.Where(a => a.Customer_ID == item.Customer_ID).Single();
                    query.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "KPI").SingleOrDefault().Status_ID;
                    
                }
                db.SaveChanges();
                
            }
            catch
            {
                throw;
            }
        }

        public void UpdateCustomer(CustomerEntity cus, ref string message)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                UserInfoEntity us = new UserInfoEntity();
                var query = db.Customers.Where(a => a.Customer_ID == cus.Customer_ID).SingleOrDefault();
                    query.FirstName = cus.FirstName;
                    query.LastName = cus.LastName;
                    query.Note = cus.Note;
                    query.EDD = cus.EDD;
                    query.IDNumber = cus.IDNumber;
                    query.DateOfBirth = cus.DateOfBirth;
                    query.HomeNumber = cus.HomeNumber;
                    query.Substreet = cus.SubStreet;
                    query.Street = cus.Street;
                    query.Ward_Code = cus.Ward_Code;
                    query.District_Code = cus.District_Code;
                    query.Province_Code = cus.Province_Code;
                    query.HomePhoneNumber = cus.HomePhoneNumber;
                    query.OfficePhoneNumber = cus.OfficePhoneNumber;
                    query.CellPhoneNumber = cus.CellPhoneNumber;
                    query.CurrentBrandName = (int)cus.CurrentBrandName;
                    query.PreviousBrandName = cus.PreviousBrandName;
                    query.LastUpdatedBy = us.UserID;
                    db.SaveChanges();
            }
            catch
            {
                message = "Có vấn đề xảy ra trong quá trình cập nhật.\nXin vui lòng thực hiện lại.";
            }
        }

        public CallDetailEntity ExecuteCall(CallDetailEntity calldetail, ref string message)
        {
            try
            {
                CRMEntities db = new CRMEntities();
                OutBoundDetail call = new OutBoundDetail();
                var query = (from dw in db.DividingWorkings
                             where dw.Customer_ID == calldetail.Customer_ID && dw.User_ID == calldetail.User_ID
                             select dw).SingleOrDefault();
                query.TimeEndCall = DAL.ServerDateTime.SystemDateTime;
                if (calldetail.EndStatus != true)
                {
                    call.Customer_ID = calldetail.Customer_ID;
                    call.User_ID = calldetail.User_ID;
                    call.CallStatus_ID = calldetail.CallStatus_ID;
                    call.ResultGroup = calldetail.ResultGroup;
                    call.ResultDetail = calldetail.ResultDetail;
                    call.Note = calldetail.Note;
                    call.EndStatus = calldetail.EndStatus;
                    call.CallDate = DAL.ServerDateTime.SystemDateTime;

                    db.OutBoundDetails.Add(call);
                }
                else
                {
                    call.Customer_ID = calldetail.Customer_ID;
                    call.User_ID = calldetail.User_ID;
                    call.CallStatus_ID = calldetail.CallStatus_ID;
                    call.ResultGroup = calldetail.ResultGroup;
                    call.ResultDetail = calldetail.ResultDetail;
                    call.Note = calldetail.Note;
                    call.EndStatus = calldetail.EndStatus;
                    call.CallDate = DAL.ServerDateTime.SystemDateTime;
                    var taskquery = db.DividingWorkings.Where(a => a.Customer_ID == calldetail.Customer_ID).SingleOrDefault();
                    var cusquery = db.Customers.Where(a => a.Customer_ID == calldetail.Customer_ID).SingleOrDefault();
                    // Neu la Call 1
                    if (calldetail.CallStatus_ID == 2)
                    {
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Fo")
                        {
                            taskquery.CallStatus_ID = 3;
                            cusquery.CallStatus = 3;
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "POT").SingleOrDefault().Status_ID;
                            //db.DividingWorkings.Remove(taskquery);
                        }
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "User")
                        {
                            cusquery.CallStatus = 5;
                            taskquery.CallStatus_ID = 5;
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NON").SingleOrDefault().Status_ID;
                           // db.DividingWorkings.Remove(taskquery);
                        }
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Un")
                        {
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                            db.DividingWorkings.Remove(taskquery);
                        }
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "UP")
                        {
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                            db.DividingWorkings.Remove(taskquery);
                        }

                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "IN")
                        {
                            string _resultdetail = SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultDetail).SingleOrDefault().Result_Code;

                            if (_resultdetail == "IN1" ||
                                _resultdetail == "IN2" ||
                                _resultdetail == "IN3" ||
                                _resultdetail == "IN4" ||
                                _resultdetail == "IN12" ||
                                _resultdetail == "IN13")
                            {
                                cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                db.DividingWorkings.Remove(taskquery);
                            }                            
                        }

                    }
                    // Neu la Call 2
                     else if (calldetail.CallStatus_ID == 3)
                    {

                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "User")
                        {
                            cusquery.CallStatus = 5;
                            taskquery.CallStatus_ID = 5;
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NON").SingleOrDefault().Status_ID;
                           // db.DividingWorkings.Remove(taskquery);
                        }
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Un")
                        {
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                            db.DividingWorkings.Remove(taskquery);
                        }
                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "UP")
                        {
                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                            db.DividingWorkings.Remove(taskquery);
                        }

                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "IN")
                        {
                            string _resultdetail = SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultDetail).SingleOrDefault().Result_Code;

                            if (_resultdetail == "IN1" ||
                                _resultdetail == "IN2" ||
                                _resultdetail == "IN3" ||
                                _resultdetail == "IN4" ||
                                _resultdetail == "IN12" ||
                                _resultdetail == "IN13")
                            {
                                cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                db.DividingWorkings.Remove(taskquery);
                            }
                            else
                            {

                            }
                        }
                    }
                            else if (calldetail.CallStatus_ID == 5)
                                    {
                                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Fo")
                                        {
                                            cusquery.CallStatus = 6;
                                            taskquery.CallStatus_ID = 6;
                                        }

                                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Un")
                                        {
                                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                            db.DividingWorkings.Remove(taskquery);
                                        }
                                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "UP")
                                        {
                                            cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                            db.DividingWorkings.Remove(taskquery);
                                        }

                                        if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "IN")
                                        {
                                            string _resultdetail = SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultDetail).SingleOrDefault().Result_Code;

                                            if (_resultdetail == "IN1" ||
                                                _resultdetail == "IN2" ||
                                                _resultdetail == "IN3" ||
                                                _resultdetail == "IN4" ||
                                                _resultdetail == "IN12" ||
                                                _resultdetail == "IN13")
                                            {
                                                cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                                db.DividingWorkings.Remove(taskquery);
                                            }
                                            else
                                            {

                                            }
                                        }
                                    }
                                    else if (calldetail.CallStatus_ID == 6)
                                        {
                                            
                                            if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "User")
                                            {
                                                db.DividingWorkings.Remove(taskquery);
                                            }
                                            if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "Un")
                                            {
                                                cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                                db.DividingWorkings.Remove(taskquery);
                                            }
                                            if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "UP")
                                            {
                                                cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                                db.DividingWorkings.Remove(taskquery);
                                            }

                                            if (SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultGroup).SingleOrDefault().Result_Code == "IN")
                                            {
                                                string _resultdetail = SharedData.ResultStatusList.Where(a => a.ResultID == calldetail.ResultDetail).SingleOrDefault().Result_Code;

                                                if (_resultdetail == "IN1" ||
                                                    _resultdetail == "IN2" ||
                                                    _resultdetail == "IN3" ||
                                                    _resultdetail == "IN4" ||
                                                    _resultdetail == "IN12" ||
                                                    _resultdetail == "IN13")
                                                {
                                                    cusquery.CustomerStatus = SharedData.StatusList.Where(a => a.Status_Code == "NEV").SingleOrDefault().Status_ID;
                                                    db.DividingWorkings.Remove(taskquery);
                                                }                                                
                                            }
                                            cusquery.LastUpdatedBy = call.User_ID;
                                        }
                   
                    db.OutBoundDetails.Add(call);
                }
                db.SaveChanges();
                CallDetailEntity cde = new CallDetailEntity();
                cde.Call_ID = call.ID;
                db.SaveChanges();
                return cde;
            }
            catch 
            {
                message = "Phải thực hiện cuộc gọi xong mới có thể xác nhận.";
                return null;
            }
        }


        public void InsertSampleDetail(string SampleCode, long Call_ID, long Customer_ID, int User_ID)
        {
            CRMEntities db = new CRMEntities();
            SampleDetail sd = new SampleDetail();
            sd.Sample_ID = SharedData.SampleList.Where(a => a.Sample_Code == SampleCode).SingleOrDefault().Sample_ID;
            sd.CallDetail_ID = Call_ID;
            db.SampleDetails.Add(sd);
            db.SaveChanges();
        }



        public void UpdateSampleDetail(List<SampleDetailEntity> list)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                foreach (SampleDetailEntity item in list)
                {
                    long ID = item.SampleDetail_ID;
                    var query = db.SampleDetails.Where(a => a.ID == ID).SingleOrDefault();
                    query.Sample_ID = item.Sample_ID;
                    query.ReceivedDate = item.DateReceive;
                    query.SendStatus = item.SendStatus;
                    query.CheckedDate = item.DateCheck;
                }
                db.SaveChanges();
            }
            catch { throw; }
        }

        public void InsertSurvey(byte NumberCup, bool ProductName, int Price, 
            string BrandName, string EventCommercial, string ProductCommercial, long Call_ID)
        {
            CRMEntities db = new CRMEntities();
            Survey su = new Survey();
            su.NumberOfCup = (byte)NumberCup;
            su.ProductName = ProductName;
            su.Price = Price;
            su.BrandName = BrandName;
            su.EventDescription = EventCommercial;
            su.ProductDescription = ProductCommercial;
            su.CallDetail_ID = Call_ID;
            db.Surveys.Add(su);
            db.SaveChanges();
        }

        public void InsertUser(UserEntity user)
        {
            CRMEntities db = new CRMEntities();            
            User u = new User();
            u.LastName = user.LastName;
            u.FirstName = user.FirstName;
            u.Account = user.Account;
            u.Password = user.Password;
            u.TelephoneNumber = user.TelephoneNumber;
            u.Region_Code = user.Region_Code;
            u.Role = user.Role;
            db.Users.Add(u);
            db.SaveChanges();
        }

        public void UpdateCallRecord(RecordEntity record)
        {
            CRMEntities db = new CRMEntities();
            Record re = new Record();
            re.SourceDestination = record.SourceDestination;
            re.ReceivedDestination = record.ReceivedDestination;
            re.Type = record.Type;
            re.User_ID = record.User_ID;
            re.DateReceived = record.DateReceived;
            re.RecordLink = record.RecordLink;
            re.Duration = record.Duration;
            db.Records.Add(re);
            db.SaveChanges();
        }

        public void UpdateUserPermit(PermissionEntity permit, int userid)
        {
            CRMEntities db = new CRMEntities();
            var query = (from p in db.PermitDetails where p.User_ID == userid && p.Permission_ID == permit.Permission_ID select p).SingleOrDefault();
            if (query != null)
            {
                if (query.Permission_ID == permit.Permission_ID)
                {
                    query.Status = permit.Status;
                }                
            }
            else
            {
                    PermitDetail per = new PermitDetail();
                    per.User_ID = userid;
                    per.Permission_ID = permit.Permission_ID;
                    per.Status = permit.Status;
                    db.PermitDetails.Add(per);
            }
            db.SaveChanges();
        }

        #region Catalog_Manager
        #region Catalog_Region
        public void AddNewRegionCode(RegionEntity region)
        {
            CRMEntities db = new CRMEntities();
            Region r = new Region();
            r.RegionLevel = region.RegionLevel;
            r.RegionName = region.RegionName;
            r.RegionType = region.RegionType;
            r.Parent_Code = region.Parent_Code;
            r.Ordinal = region.Ordinal;
            db.Regions.Add(r);
            db.SaveChanges();
        }
        #endregion

        #region Catalog_Collector
        public void AddNewCollector(string Collector_Code, string FullName, int Region_ID)
        {
            CRMEntities db = new CRMEntities();
            Collector c = new Collector();
            c.Collector_Code = Collector_Code;
            c.FullName = FullName;
            c.Region_ID = Region_ID;
            db.Collectors.Add(c);
            db.SaveChanges();
        }

        public void UpdateCollector(CollectorEntity collector)
        {
            CRMEntities db = new CRMEntities();
            var query = db.Collectors.Where(a => a.ID == collector.Collector_ID).SingleOrDefault();
            query.FullName = collector.FullName;
            query.Region_ID = collector.Region_ID;
            db.SaveChanges();
        }
        #endregion
        #endregion

        public void UpdateUserInfo(UserEntity user)
        {
            CRMEntities db = new CRMEntities();
            var query = (from u in db.Users
                         where u.User_ID == user.UserID
                         select u).SingleOrDefault();
            if (query != null)
            {
                query.LastName = user.LastName;
                query.FirstName = user.FirstName;
                query.Account = user.Account;
                query.Password = user.Password;
                query.Region_Code = user.Region_Code;
                query.TelephoneNumber = user.TelephoneNumber;
                query.IsDisable = user.Disable;
            }
            db.SaveChanges();
        }

        public void AddNewResultStatus(ResultStatusEntity result, string node, ref string message)
        {
            CRMEntities db = new CRMEntities();
            ResultStatu re = new ResultStatu();
            try
            {
                switch (node)
                {
                    case "root":
                        re.Result_Code = result.Result_Code;
                        re.Result_Level = 1;
                        re.ResultName = result.ResultName;
                        re.Description = result.Description;
                        re.Parent_Code = result.Parent_Code;
                        break;

                    case "child":
                        re.Result_Code = result.Result_Code;
                        re.Result_Level = result.Result_Level + 1;
                        re.ResultName = result.ResultName;
                        re.Description = result.Description;
                        re.Parent_Code = (int)result.Parent_Code;
                        break;
                }
                db.ResultStatus.Add(re);
                db.SaveChanges();
            }
            catch
            {
                message = "Có lỗi xảy ra trong quá trình thực hiện";
            }
            finally
            {
                db.Dispose();
            }
        }

        public void UpdateResultStatus(ResultStatusEntity result, ref string message)
        {
            CRMEntities db = new CRMEntities();
            var query = (from re in db.ResultStatus
                         where re.ID == result.ResultID
                         select re).SingleOrDefault();
            try
            {
                query.Result_Code = result.Result_Code;
                query.Result_Level = result.Result_Level;
                query.ResultName = result.ResultName;
                query.Description = result.Description;
                query.IsDisable = result.IsDisable;
                db.SaveChanges();
            }
            catch
            {
                message = "Có lỗi xảy ra trong quá trình thúc hiện";
            }
        }

        public void DisableResultStatus(ResultStatusEntity result)
        { 
            CRMEntities db = new CRMEntities();
            var listchild = (from r in db.ResultStatus
                         where r.Parent_Code == result.ResultID
                         select r).ToList();
            if (listchild.Count > 0)
            {
                foreach (ResultStatu item in listchild)
                {
                    item.IsDisable = result.IsDisable;
                }                
            }
            var root = (from r in db.ResultStatus
                        where r.ID == result.ResultID
                        select r).SingleOrDefault();
            root.IsDisable = result.IsDisable;
            db.SaveChanges();
            
        }

        public void AddNewBrand(BrandEntity brand, string node)
        {
            CRMEntities db = new CRMEntities();
            Brand br = new Brand();
            try
            {
                switch (node)
                {
                    case "root":
                        br.Brand_Code = brand.Brand_Code;
                        br.BrandLevel = brand.BrandLevel;
                        br.BrandName = brand.BrandName;
                        br.Description = brand.Description;
                        br.Parent_Code = brand.Parent_Code;
                        break;

                    case "child":
                        br.Brand_Code = brand.Brand_Code;
                        br.BrandLevel = brand.BrandLevel;
                        br.BrandName = brand.BrandName;
                        br.Description = brand.Description;
                        br.Parent_Code = brand.Parent_Code;
                        break;
                }
                db.Brands.Add(br);
                db.SaveChanges();
            }
            catch
            {

            }
            finally
            {
                db.Dispose();
            }
        }

        public void UpdateBrand(BrandEntity brand)
        {
            CRMEntities db = new CRMEntities();
            var query = (from br in db.Brands
                         where br.ID == brand.Brand_ID
                         select br).SingleOrDefault();
            if (query != null)
            {
                query.Brand_Code = brand.Brand_Code;
                query.BrandName = brand.BrandName;
                query.BrandLevel = brand.BrandLevel;
                query.Description = brand.Description;
                query.Parent_Code = brand.Parent_Code;
               // query.IsCustomer = brand.IsCustomer;
                db.SaveChanges();
            }
            
            db.Dispose();
        }

        public void UpdateBrandStatus(BrandEntity brand, string stat)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                var query = (from br in db.Brands
                             where br.ID == brand.Brand_ID
                             select br).SingleOrDefault();
                if (stat == "IsCustomer")
                {
                    query.IsCustomer = brand.IsCustomer;
                }
                else if (stat == "IsDisable")
                {
                    query.IsDisable = brand.IsDisable;
                }
                db.SaveChanges();

            }
            catch
            {
                throw;
            }
            finally
            {
                db.Dispose();
            }
            
        }

        public void UpdateSample(SampleEntity _sample)
        {
            CRMEntities db = new CRMEntities();
            var query = (from sa in db.Samples
                         where sa.ID == _sample.Sample_ID 
                         select sa).SingleOrDefault();
            if (query != null)
            {
                query.ID = _sample.Sample_ID;
                query.Parent_Code = _sample.Parent_Code;
                query.SampleLevel = _sample.Sample_Level;
                query.SampleName = _sample.SampleName;
                query.Description = _sample.Description;
                query.Sample_Code = _sample.Sample_Code;

            }

            db.SaveChanges();
            db.Dispose();
            
        }


        public void AddSample(String _code, String _name, int _level, String _dct, int _pCode)
        {
            CRMEntities db = new CRMEntities();

            Sample _sample = new Sample();
            _sample.SampleName = _name;
            _sample.SampleLevel = _level;
            _sample.Description = _dct;
            _sample.Parent_Code = _pCode;
            _sample.Sample_Code = _code;

            db.Samples.Add(_sample);
            db.SaveChanges();
            db.Dispose();

        }


        public void AddIBDetail(IBDetailEntity _ib)
        {
            CRMEntities db = new CRMEntities();
            InBoundDetail ib = new InBoundDetail();
            ib.DateOfComplain = _ib.DateOfComplain;
            ib.SourceReciving = _ib.SourceReciving;
            ib.Customer_ID = _ib.Customer_ID;
            ib.NXS = _ib.NSX;
            ib.HSD = _ib.HSD;
            ib.BrandName_ID = _ib.BrandNameID;
            ib.ReasonComplain = _ib.ReasonComplain;
            ib.Status = _ib.Status;
            ib.CRMResponse = _ib.CRMResponse;
            ib.SupplyResponse = _ib.SupplyResponse;
            ib.FactoryResponse = _ib.FactoryResponse;
            ib.ProductExchange = _ib.ProductExchange;
            ib.GiftProduct = _ib.GiftProduct;
            ib.DatetoFeedBack = _ib.DatetoFeedBack;

            db.InBoundDetails.Add(ib);
            db.SaveChanges();
            db.Dispose();
            return;
        }

        public void UpdateIBDetail(IBDetailEntity _ib)
        {
            CRMEntities db = new CRMEntities();
            var query = (from ib in db.InBoundDetails
                         where ib.ID == _ib.ID
                         select ib).SingleOrDefault();
            if (query != null)
            {
                query.DateOfComplain = _ib.DateOfComplain;
                query.SourceReciving = _ib.SourceReciving;
                query.Customer_ID = _ib.Customer_ID;
                query.NXS = _ib.NSX;
                query.HSD = _ib.HSD;
                query.BrandName_ID = _ib.BrandNameID;
                query.ReasonComplain = _ib.ReasonComplain;
                query.Status = _ib.Status;
                query.CRMResponse = _ib.CRMResponse;
                query.SupplyResponse = _ib.SupplyResponse;
                query.FactoryResponse = _ib.FactoryResponse;
                query.ProductExchange = _ib.ProductExchange;
                query.GiftProduct = _ib.GiftProduct;
                query.DatetoFeedBack = _ib.DatetoFeedBack;

            }
            
            db.SaveChanges();
            db.Dispose();
            return;
        }

        public void UpdateCallSetting(int[] callsetting)
        {
            CRMEntities db = new CRMEntities();
            var query = (from call in db.SettingCalls
                         select call).ToList();
            query[0].C1 = callsetting[0];
            query[0].E1 = callsetting[1];
            query[0].E2 = callsetting[2];
            db.SaveChanges();
                
        }

        public void ChangePass(UserEntity user)
        { 
            CRMEntities db = new CRMEntities();
            var query = (from us in db.Users
                         where us.User_ID == user.UserID
                         select us).SingleOrDefault();

            query.Password = user.Password;
            db.SaveChanges();
        }

        protected void AddInBoundDetail(CustomerEntity cus,IBDetailEntity _ib, RecordEntity record)
        {
            
            
        }


      
    }
}
